<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/24 17:35
 * Desc: 秒杀服务
 */
class Service_Api_Marketing_Seckill extends Contract_Api_Marketing_Seckill
{

    public function list($params): array
    {
        $status = Arr::get($params, 'status');
        $page = Arr::get($params, 'page', 1);
        $pagesize = Arr::get($params, 'pagesize', 10);

        $offset = St_Page::offset($page, $pagesize);

        $date = time();

        $builder = DB::select()->from('seckill')
            ->where('status', '!=', 2)
            ->where('number', '>', 0);
        if($params['keyword']){
            $builder->and_where(DB::expr(" AND title LIKE '%{$params['keyword']}%'"));
        }
        if ($status == 1) {
            $builder->where('start_date', '<', $date)
                ->where('end_date', '>', $date)
                ->order_by('end_date', 'desc');
        }else {
            $builder->where('start_date', '>', $date)
                ->where('end_date', '>', $date)
                ->order_by('id', 'desc');
        }

        $list = $builder->offset($offset)->limit($pagesize)->execute()->as_array();

        $data = array();
        foreach($list as $k=>&$v){
            if($v['start_date']>time() && $v['end_date']>time()){
                $data[$k]['remaining_time'] = ($v['start_date']-time())*1000;
            }else if($v['end_date']>time() && $v['start_date']<time()){
                $data[$k]['remaining_time'] = ($v['end_date']-time())*1000;
            }
            $data[$k]['id'] = $v['id'];
            if($v['type_id']){
                $data[$k]['type_name'] = DB::select()->from('model')->where('id','=',$v['type_id'])->execute()->get('modulename');
            }
            $data[$k]['remain_number'] = $v['number'];
            $data[$k]['title'] = $v['title'];
            $data[$k]['price'] = Model_Seckill::get_price($v);//获取单个价格
            $data[$k]['original_price'] = Model_Seckill::get_original_price($v);//获取原价
            $info = Model_Seckill::get_product_litpic($v);//获取封面图
            $data[$k]['litpic'] = $this->img_url($info['litpic']);
            //计算每个订单有多少个秒杀产品数
            $data[$k]['sales_volumes'] = $this->get_seckill_sell_num($v['id']);
            $data[$k]['percentage'] = sprintf("%.2f", $data[$k]['sales_volumes'] / ($v['number']+$data[$k]['sales_volumes']));
            $data[$k]['percentage'] = ($data[$k]['percentage']*100).'%';
            $data[$k]['total_number'] = $v['number']+$data[$k]['sales_volumes'];
            $data[$k]['icon_list'] = Product::get_ico_list($v['iconlist']);
            $data[$k]['attrlist'] = Model_Seckill_Attr::get_attr_list($v['attrid']);
        }

       return $data;
    }

    public function show($params): array
    {
        $id = Arr::get($params, 'id');
        $mid = Arr::get($params, 'mid', '');

        $info     = DB::select()
            ->from('seckill')
            ->where('id', '=', $id)
            ->execute()
            ->current();
        $pro_info = Model_Seckill::get_product_litpic($info);

        /* 图片分割 */
        if ($pro_info['piclist']) {
            $arr = explode(',', $pro_info['piclist']);
            foreach ($arr as $k => $v) {
                $img     = explode('||', $v);
                $arr[$k] = $this->img_url($img[0]);
            }
            $piclist = $arr;
        } else {
            $piclist = array($this->img_url($pro_info['litpic']));
        }

        /* 秒杀状态 */
        if ($info['start_date'] > time() && $info['end_date'] > time()) {
            $status         = 2;
            $remaining_time = ($info['start_date'] - time()) * 1000;
        } else if ($info['end_date'] > time() && $info['start_date'] < time()) {
            $status         = 1;
            $remaining_time = ($info['end_date'] - time()) * 1000;
        }
        $suit_info = Model_Seckill::get_suit_info($info['type_id'], $info);//获取套餐信息

        $seckill_order = true;
        $login         = false;
        /* 登陆了才能预定 */
        if ($mid && $info['id_purchase_limits'] > 0) {
            $num           = DB::select()->from('seckill_order')->where("mid={$mid} and seckill_id={$info['id']}")->execute()->count();
            $seckill_order = ($info['id_purchase_limits'] < $num || $info['id_purchase_limits'] == $num) ? false : true;
            $login         = true;
        }

        $discount     = explode(',', $info['discount']);
        $discount_arr = array(
            'integral' => in_array(1, $discount) ? 1 : 0,
            'coupon' => in_array(2, $discount) ? 1 : 0,
            'red_envelopes' => in_array(3, $discount) ? 1 : 0,
        );
        $start_time   = '';
        $end_time     = '';
        if ($info['type_id'] == 2 || $info['type_id'] == 3) {
            $today = strtotime(date('Y-m-d', time()));
            $date  = explode(',', $info['suit_date']);
            if ($date[0] > $today || $date[0] = $today) {
                $start_time = $date[0];
                $end_time   = $info['type_id'] == 3 ? $date[0] : $date[0] + 86400;
            } else {
                $start_time = $today;
                $end_time   = $info['type_id'] == 3 ? $today : $today + 86400;
            }
        }
        $data                 = array(
            'id' => $info['id'],
            'aid' => $info['aid'],
            'seckill_title' => $info['title'],
            'title' => $pro_info['title'],
            'piclist' => $piclist,
            'start_date' => $info['start_date'],
            'end_date' => $info['end_date'],
            'status' => $status,
            'describe' => $this->content_add_domain($info['describe']),//描述
            'product_id' => $info['product_id'],
            'type_id' => $info['type_id'],
            'original_price' => Model_Seckill::get_original_price($info),/* 获取原价 */
            'price' => Model_Seckill::get_price($info),/* 获取最低价格 */
            'adult_price' => $info['adult_price'],
            'child_price' => $info['child_price'],
            'old_price' => $info['old_price'],
            'people4_price' => $info['people4_price'],
            'people5_price' => $info['people5_price'],
            'people6_price' => $info['people6_price'],
            'suit_date' => $info['suit_date'],
            'suit_end_date' => $info['suit_end_date'],
            'suit_id' => $info['suit_id'],
            'number' => $info['number'],
            'remain_number' => $info['number'],
            'sales_volumes' => $this->get_seckill_sell_num($info['id']),
            'remaining_time' => $remaining_time,
            'suit_name' => $suit_info[0][$suit_info[1]],
            'purchase_limits' => $info['purchase_limits'],
            'id_purchase_limits' => $info['id_purchase_limits'],
            'id_purchase_limits_status' => $seckill_order,
            'login' => $login,
            'discount' => $discount_arr,
            'day_num' => 1,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'product_series' => St_Product::product_series($info['product_id'], $info['type_id']),
            'use_type' => $info['type_id']==3?$pro_info['use_type']:'',
            'car_end_time'=>$info['type_id']==3?date('Y-m-d',strtotime('+1 day')):''
        );
        $data['total_number'] = $data['number'] + $data['sales_volumes'];

        return $data;
    }

    public function get_seckill_sell_num($id)
    {
        //计算每个订单总共有多少个秒杀产品数
        $seckills      = DB::select()->from('seckill_order')->where("seckill_id={$id}")->execute()->as_array();
        $sales_volumes = 0;
        foreach ($seckills as $_k => $_v) {
            $total         = 'SUM(ifnull(dingnum,0)+ifnull(childnum,0)+ifnull(oldnum,0)+ifnull(people4_num,0)+ifnull(people5_num,0)+ifnull(people6_num,0))';
            $sql           = "SELECT $total as dd FROM `sline_member_order` WHERE status in(0,1,2,5,6) and ordersn={$_v['ordersn']}";
            $ar            = DB::query(1, $sql)->execute()->current();
            $sales_volumes += $ar['dd'] ? intval($ar['dd']) : 0;
        }
        return $sales_volumes;
    }
}